package com.jinglin.admin.controller;

import com.jinglin.component.RedisComponent;
import com.jinglin.controller.ABaseController;
import com.jinglin.entity.config.AppConfig;
import com.jinglin.entity.constants.Constants;
import com.jinglin.entity.dto.TokenUserInfoDto;
import com.jinglin.entity.vo.ResponseVO;
import com.jinglin.exception.BusinessException;
import com.wf.captcha.ArithmeticCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

@RestController
@RequestMapping("/account")
@Validated
public class AccountController extends ABaseController {
    @Resource
    private RedisComponent redisComponent;
    @Resource
    private AppConfig appConfig;
    @PostMapping("/checkCode")
    public ResponseVO checkCode(){
        // 生成一个验证码图片
        ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(100, 42);
        String code = arithmeticCaptcha.text();
        String checkCodeBase64 = arithmeticCaptcha.toBase64();
        // 将验证码的值存入session
        String key = redisComponent.saveCheckCode(code);
        HashMap<String,String> map = new HashMap<>();
        map.put("checkCode",checkCodeBase64);
        map.put("checkCodeKey",key);
        // 把验证码图片返回给前端
        return getSuccessResponseVO(map);
    }
    @RequestMapping("/login")
    public ResponseVO login(HttpServletResponse response,
                            @NotEmpty  String account,
                            // 注意这里穿过来的password已经在前端进行了md5加密
                            @NotEmpty  String password,
                            @NotEmpty  String checkCodeKey,
                            @NotEmpty  String checkCode){
        String  myCheckCode = redisComponent.getCheckCode(checkCodeKey);
        try {
            if (!myCheckCode.equalsIgnoreCase(checkCode)){
                throw new BusinessException("图形验证码不正确");
            }
            if (!appConfig.getAdminAccount().equals(account) || !(DigestUtils.md5DigestAsHex(appConfig.getAdminPassword().getBytes(StandardCharsets.UTF_8))).equals(password)){
                return getBusinessErrorResponseVO(new BusinessException("账号密码错误"),null);
            }
            String token = redisComponent.saveAdminTokenInfo(account);
            saveAdminToken2Cookie(response,token);
            return getSuccessResponseVO("登录成功");
        }finally {
            redisComponent.cleanCheckCode(checkCodeKey);
        }
    }
    @RequestMapping("/logout")
    public ResponseVO logout(HttpServletResponse response){
        cleanCookie(response,Constants.TOKEN_ADMIN);
        return getSuccessResponseVO("退出成功！！");
    }

}
